package com.chenyongjing.design.pattern.interpreter.calculate;

import java.util.Stack;

/**
 * @Author chen
 * @Date 2022/11/27 14:33
 */
public class GPCalculator {

    private Stack<IArithmeticInterpreter> stack =new Stack<IArithmeticInterpreter>();
    public GPCalculator(String expression){
        parse(expression);
    }

    /**
     * 解析方法
     * @param expression
     */
    private void parse(String expression){
        String[] elements = expression.split(" ");
        IArithmeticInterpreter left,right;
        for (int i=0;i<elements.length;i++){
            String operator = elements[i];
            if (OperatorUtil.ifOperator(operator)){
                left = this.stack.pop();
                right = new NumInterpreter(Integer.valueOf(elements[++i]));
                System.out.println("出栈"+left.interpret()+"和"+right.interpret());
                this.stack.push(OperatorUtil.getInterpreter(left,right,operator));
                System.out.println("应用运算符"+operator);
            }else{
                NumInterpreter numInterpreter = new NumInterpreter(Integer.valueOf(elements[i]));
                this.stack.push(numInterpreter);
                System.out.println("入栈："+numInterpreter.interpret());

            }
        }
    }

    public int callulate(){
        return this.stack.pop().interpret();
    }
}
